package org.vergil.core.dao.impl;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import org.vergil.core.dao.BaseRepository;
import org.vergil.core.dao.UserRepository;
import org.vergil.dto.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * Created by vergil on 17-4-5.
 */
@Repository
public class UserRepositoryImpl extends BaseRepository implements UserRepository {
    @Override
    public Long insert(User user) {
        String sql = "INSERT INTO user(username, password, status) VALUES (:username,:password,:status)";
        KeyHolder keyHolder = new GeneratedKeyHolder();//创建一个主键持有者
        SqlParameterSource parameterSource = new BeanPropertySqlParameterSource(user);
        nameTemplate.update(sql, parameterSource, keyHolder);
        Long userId = keyHolder.getKey().longValue();
        return userId;
    }

    @Override
    public int update(User user) {
        if (null == user.getId() || user.getId().equals(0L)) {
            return 0;
        }
        StringBuilder sql = new StringBuilder(" UPDATE user SET ");
        if (null != user.getUsername() || "".equals(user.getUsername())) {
            sql.append(" username = :username ");
        }
        if (null != user.getPassword() || "".equals(user.getPassword())) {
            sql.append(", password = :password ");
        }
        if (null != user.getStatus() || 0 == user.getStatus()) {
            sql.append(", status = :status ");
        }
        sql.append(" WHERE id = :id ");
        SqlParameterSource parameterSource = new BeanPropertySqlParameterSource(user);
        return nameTemplate.update(sql.toString(), parameterSource);
    }

    @Override
    public int delete(Long userId) {
        String sql = " DELETE FROM user WHERE id = :id ";
        SqlParameterSource parameterSource = new MapSqlParameterSource("id", userId);
        return nameTemplate.update(sql, parameterSource);
    }

    @Override
    public User getUser(Long userId) {
        String sql = " SELECT id,username,password,status FROM user WHERE id = :id ";
        SqlParameterSource parameterSource = new MapSqlParameterSource("id", userId);
        return nameTemplate.queryForObject(sql, parameterSource, new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet resultSet, int i) throws SQLException {
                return toUser(resultSet);
            }
        });
    }

    @Override
    public User getUser(String username, String password) {
        String sql = " SELECT id,username,password,status FROM sso_user "
                + " WHERE username = :username AND password =:password ";
        MapSqlParameterSource parameterSource = new MapSqlParameterSource();
        parameterSource.addValue("username", username);
        parameterSource.addValue("password", password);
        return nameTemplate.queryForObject(sql, parameterSource, new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet resultSet, int i) throws SQLException {
                return toUser(resultSet);
            }
        });
    }

    @Override
    public List<User> getUsers(Map<String, Object> param) {
        return null;
    }


    private User toUser(ResultSet resultSet) throws SQLException {
        User user = new User();
        user.setId(resultSet.getLong("id"));
        user.setUsername(resultSet.getString("username"));
        user.setPassword(resultSet.getString("password"));
        user.setStatus(resultSet.getInt("status"));
        return user;
    }
}
